﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using AVIS.OutBound.Notify.Cache;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.Notify
{
    /// <summary>
    /// 消息通知的领域服务
    /// </summary>
    public class NotifyManager : DomainService, INotifyManager
    {
        #region 构造函数
        private readonly IRepository<MailInfo> _MailInfoRepository;
        private readonly IRepository<SMSInfo> _SMSInfoRepository;
        private readonly IRepository<MailType> _MailTypeRepository;
        private readonly IObjectMapper _objectMapper;
        private readonly ICacheManager _cacheManager;

        public NotifyManager(IRepository<MailInfo> MailInfoRepository,
            IRepository<SMSInfo> SMSInfoRepository,
            IObjectMapper objectMapper,
            ICacheManager cacheManager,
            IRepository<MailType> MailTypeRepository)
        {
            _MailInfoRepository = MailInfoRepository;
            _SMSInfoRepository = SMSInfoRepository;
            _objectMapper = objectMapper;
            _cacheManager = cacheManager;
            _MailTypeRepository = MailTypeRepository;

        }
        #endregion


        /// <summary>
        /// 发送邮件（保存到数据库，由定时任务发送）
        /// </summary>
        /// <param name="email">邮箱</param>
        /// <param name="TempletName">模板名称</param>
        /// <param name="templateParameters">变量集合</param>
        /// <returns></returns>
        public bool SendMail(string email, string TempletName, string templateParameters)
        {
            if (string.IsNullOrWhiteSpace(email))
            {
                Logger.Debug("发送邮件时，电子邮件不能为空！");
                return false;
            }

            var mail = new MailInfo();
            mail.SendTo = email;
            mail.TempletName = TempletName;
            mail.Subject = GetMail(mail.TempletName).Subject;
            mail.TempleParameters = templateParameters;
            mail.FailFlag = false;
            mail.SendFlag = false;
            mail.SendTime = DateTime.Now;

            _MailInfoRepository.Insert(mail);
            return true;
        }

        /// <summary>
        /// 发送SMS（保存到数据库，由定时任务发送）
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <param name="TempletName">模板名称</param>
        /// <param name="TempleParameters">变量集合</param>
        /// <returns></returns>
        public bool SendSMS(string mobile, string TempletName, string TempleParameters)
        {
            if (string.IsNullOrWhiteSpace(mobile))
            {
                Logger.Debug("发送短信时，手机号码不能为空！");
                return false;
            }

            //我看PC逻辑也是往数据库中插入数据，于是做了修改 邓训2017-11-29
            var sms = new SMSInfo();
            sms.SendFlag = false;
            sms.ReturnFlag = false;
            sms.SendTo = mobile;
            sms.SendTime = DateTime.Now;
            sms.TempletName = TempletName;
            sms.TempleParameters = TempleParameters;
            sms.MessageContents = "";
            _SMSInfoRepository.Insert(sms);

            return true;
        }

        /// <summary>
        /// 获取缓存中的数据
        /// </summary>
        /// <param name="TempletName">模板名称</param>
        /// <returns></returns>
        private MailTypeCacheItem GetMail(string TempletName)
        {
            var cacheitem = _cacheManager.GetMailTypeCache().Get(TempletName, K => GetMailInfo(TempletName));
            return cacheitem;
        }

        /// <summary>
        /// 从数据库中获取数据
        /// </summary>
        /// <param name="TempletName">模板名称</param>
        /// <returns></returns>
        private MailTypeCacheItem GetMailInfo(string TempletName)
        {
            var data = _MailTypeRepository.FirstOrDefault(t => t.TempletName == TempletName);
            var Mail = _objectMapper.Map<MailTypeCacheItem>(data);
            return Mail;

        }
    }
}
